package com.listen.common;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.apache.commons.lang.text.StrBuilder; 

/**
 * 图片转成十六进制
 */ 
public class ImageToHex {
    public static void main(String[] args) throws IOException{ 
    	//System.out.println(getHexStrByRGB("D://photos//image//019818220795.jpg"));
    	//System.out.println(UUID.randomUUID().toString());
    	new Timer().schedule(new TimerTask() {
    		int i = 1;
			@Override
	        public void run() {
	            System.out.println(i++);
	        }
	    }, 0, 1000);
    }
    
    public static String getHexStrByRGB(String picName) throws IOException {
    	StringBuilder sb = new StringBuilder();
    	File file = new File(picName);
    	BufferedImage image = ImageIO.read(file);
	    int iw = image.getWidth();
	    int ih = image.getHeight();
	    for (int y = 0; y < ih; y++) {
	    	for (int x = 0; x < iw; x++) {
		        int pixel = image.getRGB(x, y);
		        int red = (pixel >> 16) & 0xFF;
		        int green = (pixel >> 8) & 0xFF;
		        int blue = pixel & 0xFF;
		        //System.out.println("("+red+","+green+","+blue +") --> "+ toHex(red,green,blue));
		        sb.append(toHex(red,green,blue) + ",");
	    	}
		}
	    String str = sb.toString();
	    /*PrintWriter pw = new PrintWriter(new FileWriter("D://photos//image//C.txt"));    
        pw.println(str);    
        pw.close();*/
	    return str;
    }
    
    public static String toHex(int r, int g, int b) { 
    	return toBrowserHexValue(r) + toBrowserHexValue(g) + toBrowserHexValue(b); 
	}
    
    private static String toBrowserHexValue(int number) {
    	StringBuilder builder = new StringBuilder(Integer.toHexString(number & 0xff)); 
    	while (builder.length() < 2) {
    		builder.append("0");
    	} 
    	return builder.toString().toUpperCase(); 
	}
    
	/*public static String getHexStr(String picName) {
    	FileInputStream fis = null;
    	BufferedInputStream bis = null;
    	ByteArrayOutputStream bos = null;
        String str = null;
    	try{
    		fis = new FileInputStream(picName);
    		bis = new BufferedInputStream(fis);                                               
    		bos = new java.io.ByteArrayOutputStream();
	        byte[] buff=new byte[1024]; 
	        int len=0; 
	        while((len=fis.read(buff))!=-1){
	            bos.write(buff,0,len); 
	        } 
	        //得到图片的字节数组 
	        byte[] result = bos.toByteArray(); 
	        //字节数组转成十六进制 
	        str = bytesToHexString(result); 
	        *//**
	         * 	将十六进制串保存到txt文件中
	         *//* 
	        //PrintWriter pw = new PrintWriter(new FileWriter("D://photos//image//C.txt"));    
	        //pw.println(str);    
	        //pw.close();
    	}catch(IOException e){
    		e.printStackTrace();
    	}finally {
    		try {
				fis.close();
				bis.close();
	    		fis.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
    	}
    	return str;
    }
    
    *//**实现字节数组向十六进制的转换方法一
     *//* 
    public static String byte2HexStr(byte[] b) { 
        String hs=""; 
        String stmp=""; 
        for (int n=0;n<b.length;n++) { 
            stmp=(Integer.toHexString(b[n] & 0XFF)); 
            if (stmp.length()==1) hs=hs+"0"+stmp; 
            else hs=hs+stmp; 
        } 
        return hs.toLowerCase(); 
    } 

    *//**实现字节数组向十六进制的转换的方法二
     *//* 
    public static String bytesToHexString(byte[] src){ 
        StringBuilder stringBuilder = new StringBuilder(""); 
        if (src == null || src.length <= 0) { 
            return null; 
        } 
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF; 
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0); 
            }
            stringBuilder.append(hv);
            if((i+1)%3 == 0) {
            	stringBuilder.append(",");
            }
        } 
        return stringBuilder.toString(); 
    }    */
}
